
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pio.action.administrador.inscripcion.utils;

import java.util.ArrayList;

/**
 * @param title nombre de la seccion del test
 * @param tests lista de pruebas que existen por examen
 * @param percentage porcentaje o peso global del examen, valor por defecto 100
 * @author linda perez 
 */
public class Exam {
    
private String title;
private ArrayList tests=null;
private int overallPercentage=100;   

    

    /**
     * @return the title
     */
    public Exam(String title, ArrayList tests, int overallScale, 
            int overallPercentage) {
    this.title=title;
    this.tests=tests;
    this.overallPercentage=overallPercentage;
    }
    
    public Exam(String title, ArrayList tests) {
    this.title=title;
    this.tests=tests;
       }
    
    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }


    /**
     * @return the overallPercentage
     */
    public int getOverallPercentage() {
        return overallPercentage;
    }

    /**
     * @param overallPercentage the overallPercentage to set
     */
    public void setOverallPercentage(int overallPercentage) {
        this.overallPercentage = overallPercentage;
    }

    /**
     * @return the tests
     */
    public ArrayList getTests() {
        return tests;
    }

    /** 
     * Se asignan los test pero se verifica que la suma de los
     * pesos o porcentajes de las secciones de todos los test
     * sumen overallpercent
     * @param tests the tests to set
     */
    public void setTests(ArrayList tests) { 
       int sumPer=0;
       ArrayList sections;
       Section sec;
       for(int k=0;k<tests.size();k++){
            Test test=(Test)tests.get(k);
            sections= test.getSections();
            for(int i=0;i<sections.size();i++){
                sec=(Section)sections.get(i);
                sumPer=sec.getPercentage()+sumPer;
            }
            if(sumPer>this.overallPercentage){
            System.out.println("La suma de los porcentajes debe dar "
                    +this.overallPercentage+ "Valor Erroneo: ("+sumPer+")");
            }
       } 
       this.tests = tests;
    }

    /**     
     * Se calcula la nota global en funcion a los porcentajes de
     * todas secciones de las pruebas del examen a partir de un arreglo
     * de calificaciones
     * @param rates [] arreglo de calificaciones
     * @return calificacion global o -1 en caso de existir un error
     */
        public int calculateGlobalRate(ArrayList rates, String datos) {
        int length=rates.size(),l=0;
        int totalRate=0, rateSec;
        int rate; 
        ArrayList testsL=this.tests;
        ArrayList sections;
        Section section;
        Test test;
        for(int k=0;k<testsL.size();k++){
            test=(Test)testsL.get(k);
            sections= test.getSections();
            for(int i=0;i<sections.size();i++){
                rate = (Integer)rates.get(l);
                section = (Section)sections.get(i);
                rateSec=section.calculateScale(rate, 
                         section.getPercentage());
                totalRate=totalRate+rateSec;
                l++;
                    }
        }
        if(l!=length){
        System.out.println("\nAdvertencia: El numero de notas debe corresponder al "
                + "numero de secciones. Posible cédula duplicada "+ datos);
       // System.exit(-1);
        }
        return totalRate;
    }
    
     /**     
     * Se verifica que los porcentajes de las secciones sumen overallpercent
     * @return -1 si ocurrio algun error
     */
    
    public int verifyPercentsSections() {
        int total, totalG=0;
        for(int k=0; k<this.tests.size();k++){
            Test test=(Test)this.tests.get(k);   
            ArrayList sections =test.getSections();
            for(int i=0; i<sections.size();i++){
                Section  section=(Section)sections.get(i);
                total=section.getPercentage();
                totalG=totalG+total;
            }
        }
        if(totalG!=this.overallPercentage){
            return -1;   
        }else{
            return totalG;   
        }
    }

}
